<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="限时折扣名称" prop="discountName">
        <el-input
          v-model="queryParams.discountName"
          placeholder="请输入限时折扣名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限时折扣标签" prop="discountTitle">
        <el-input
          v-model="queryParams.discountTitle"
          placeholder="请输入限时折扣标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限时折扣开始时间" prop="discountStartDatetime">
        <el-date-picker clearable size="small"
          v-model="queryParams.discountStartDatetime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择限时折扣开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="限时折扣结束时间" prop="discountEndDatetime">
        <el-date-picker clearable size="small"
          v-model="queryParams.discountEndDatetime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择限时折扣结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="限时折扣周期类型" prop="discountCycleType">
        <el-select v-model="queryParams.discountCycleType" placeholder="请选择限时折扣周期类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="周期开始时间" prop="cycleStartTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.cycleStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择周期开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="周期结束时间" prop="cycleEndTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.cycleEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择周期结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="周期周一" prop="cycleMondayUsed">
        <el-input
          v-model="queryParams.cycleMondayUsed"
          placeholder="请输入周期周一"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="周期周二" prop="cycleTuesdayUsed">
        <el-input
          v-model="queryParams.cycleTuesdayUsed"
          placeholder="请输入周期周二"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="周期周三" prop="cycleWednesdayUsed">
        <el-input
          v-model="queryParams.cycleWednesdayUsed"
          placeholder="请输入周期周三"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="周期周四" prop="cycleThursdayUsed">
        <el-input
          v-model="queryParams.cycleThursdayUsed"
          placeholder="请输入周期周四"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="周期周五" prop="cycleFridayUsed">
        <el-input
          v-model="queryParams.cycleFridayUsed"
          placeholder="请输入周期周五"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="周期周六" prop="cycleSaturdayUsed">
        <el-input
          v-model="queryParams.cycleSaturdayUsed"
          placeholder="请输入周期周六"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="周期周日" prop="cycleSundayUsed">
        <el-input
          v-model="queryParams.cycleSundayUsed"
          placeholder="请输入周期周日"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日" prop="cycleDay">
        <el-input
          v-model="queryParams.cycleDay"
          placeholder="请输入日"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限购类型" prop="discountQuotaType">
        <el-select v-model="queryParams.discountQuotaType" placeholder="请选择限购类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="限额" prop="discountQuotaRule">
        <el-input
          v-model="queryParams.discountQuotaRule"
          placeholder="请输入限额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款订单数" prop="totalOrders">
        <el-input
          v-model="queryParams.totalOrders"
          placeholder="请输入付款订单数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入付款金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抹去角和分" prop="selectedWipeZero">
        <el-input
          v-model="queryParams.selectedWipeZero"
          placeholder="请输入抹去角和分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抹去分" prop="selectedWipeCents">
        <el-input
          v-model="queryParams.selectedWipeCents"
          placeholder="请输入抹去分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否失效(0:未失效，1:已经失效)" prop="isInvalid">
        <el-input
          v-model="queryParams.isInvalid"
          placeholder="请输入是否失效(0:未失效，1:已经失效)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否新用户0否1是" prop="newMember">
        <el-input
          v-model="queryParams.newMember"
          placeholder="请输入是否新用户0否1是"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册时间条件0否1是" prop="memberRegister">
        <el-input
          v-model="queryParams.memberRegister"
          placeholder="请输入注册时间条件0否1是"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册开始时间范围" prop="memberRegisterStime">
        <el-date-picker clearable size="small"
          v-model="queryParams.memberRegisterStime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择注册开始时间范围">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="注册结束时间范围" prop="memberRegisterEtime">
        <el-date-picker clearable size="small"
          v-model="queryParams.memberRegisterEtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择注册结束时间范围">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:discount:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:discount:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:discount:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:discount:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="discountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="限时折扣编号" align="center" prop="discountId" />
      <el-table-column label="限时折扣名称" align="center" prop="discountName" />
      <el-table-column label="限时折扣标签" align="center" prop="discountTitle" />
      <el-table-column label="限时折扣开始时间" align="center" prop="discountStartDatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.discountStartDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="限时折扣结束时间" align="center" prop="discountEndDatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.discountEndDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="限时折扣周期类型" align="center" prop="discountCycleType" />
      <el-table-column label="周期开始时间" align="center" prop="cycleStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cycleStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="周期结束时间" align="center" prop="cycleEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cycleEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="周期周一" align="center" prop="cycleMondayUsed" />
      <el-table-column label="周期周二" align="center" prop="cycleTuesdayUsed" />
      <el-table-column label="周期周三" align="center" prop="cycleWednesdayUsed" />
      <el-table-column label="周期周四" align="center" prop="cycleThursdayUsed" />
      <el-table-column label="周期周五" align="center" prop="cycleFridayUsed" />
      <el-table-column label="周期周六" align="center" prop="cycleSaturdayUsed" />
      <el-table-column label="周期周日" align="center" prop="cycleSundayUsed" />
      <el-table-column label="日" align="center" prop="cycleDay" />
      <el-table-column label="限购类型" align="center" prop="discountQuotaType" />
      <el-table-column label="限额" align="center" prop="discountQuotaRule" />
      <el-table-column label="付款订单数" align="center" prop="totalOrders" />
      <el-table-column label="付款金额" align="center" prop="totalAmount" />
      <el-table-column label="抹去角和分" align="center" prop="selectedWipeZero" />
      <el-table-column label="抹去分" align="center" prop="selectedWipeCents" />
      <el-table-column label="是否失效(0:未失效，1:已经失效)" align="center" prop="isInvalid" />
      <el-table-column label="是否新用户0否1是" align="center" prop="newMember" />
      <el-table-column label="注册时间条件0否1是" align="center" prop="memberRegister" />
      <el-table-column label="注册开始时间范围" align="center" prop="memberRegisterStime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.memberRegisterStime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册结束时间范围" align="center" prop="memberRegisterEtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.memberRegisterEtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:discount:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:discount:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改限时折扣对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="限时折扣名称" prop="discountName">
          <el-input v-model="form.discountName" placeholder="请输入限时折扣名称" />
        </el-form-item>
        <el-form-item label="限时折扣标签" prop="discountTitle">
          <el-input v-model="form.discountTitle" placeholder="请输入限时折扣标签" />
        </el-form-item>
        <el-form-item label="限时折扣开始时间" prop="discountStartDatetime">
          <el-date-picker clearable size="small"
            v-model="form.discountStartDatetime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择限时折扣开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="限时折扣结束时间" prop="discountEndDatetime">
          <el-date-picker clearable size="small"
            v-model="form.discountEndDatetime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择限时折扣结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="限时折扣周期类型" prop="discountCycleType">
          <el-select v-model="form.discountCycleType" placeholder="请选择限时折扣周期类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="周期开始时间" prop="cycleStartTime">
          <el-date-picker clearable size="small"
            v-model="form.cycleStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择周期开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="周期结束时间" prop="cycleEndTime">
          <el-date-picker clearable size="small"
            v-model="form.cycleEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择周期结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="周期周一" prop="cycleMondayUsed">
          <el-input v-model="form.cycleMondayUsed" placeholder="请输入周期周一" />
        </el-form-item>
        <el-form-item label="周期周二" prop="cycleTuesdayUsed">
          <el-input v-model="form.cycleTuesdayUsed" placeholder="请输入周期周二" />
        </el-form-item>
        <el-form-item label="周期周三" prop="cycleWednesdayUsed">
          <el-input v-model="form.cycleWednesdayUsed" placeholder="请输入周期周三" />
        </el-form-item>
        <el-form-item label="周期周四" prop="cycleThursdayUsed">
          <el-input v-model="form.cycleThursdayUsed" placeholder="请输入周期周四" />
        </el-form-item>
        <el-form-item label="周期周五" prop="cycleFridayUsed">
          <el-input v-model="form.cycleFridayUsed" placeholder="请输入周期周五" />
        </el-form-item>
        <el-form-item label="周期周六" prop="cycleSaturdayUsed">
          <el-input v-model="form.cycleSaturdayUsed" placeholder="请输入周期周六" />
        </el-form-item>
        <el-form-item label="周期周日" prop="cycleSundayUsed">
          <el-input v-model="form.cycleSundayUsed" placeholder="请输入周期周日" />
        </el-form-item>
        <el-form-item label="日" prop="cycleDay">
          <el-input v-model="form.cycleDay" placeholder="请输入日" />
        </el-form-item>
        <el-form-item label="限购类型" prop="discountQuotaType">
          <el-select v-model="form.discountQuotaType" placeholder="请选择限购类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="限额" prop="discountQuotaRule">
          <el-input v-model="form.discountQuotaRule" placeholder="请输入限额" />
        </el-form-item>
        <el-form-item label="付款订单数" prop="totalOrders">
          <el-input v-model="form.totalOrders" placeholder="请输入付款订单数" />
        </el-form-item>
        <el-form-item label="付款金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入付款金额" />
        </el-form-item>
        <el-form-item label="抹去角和分" prop="selectedWipeZero">
          <el-input v-model="form.selectedWipeZero" placeholder="请输入抹去角和分" />
        </el-form-item>
        <el-form-item label="抹去分" prop="selectedWipeCents">
          <el-input v-model="form.selectedWipeCents" placeholder="请输入抹去分" />
        </el-form-item>
        <el-form-item label="是否失效(0:未失效，1:已经失效)" prop="isInvalid">
          <el-input v-model="form.isInvalid" placeholder="请输入是否失效(0:未失效，1:已经失效)" />
        </el-form-item>
        <el-form-item label="是否新用户0否1是" prop="newMember">
          <el-input v-model="form.newMember" placeholder="请输入是否新用户0否1是" />
        </el-form-item>
        <el-form-item label="注册时间条件0否1是" prop="memberRegister">
          <el-input v-model="form.memberRegister" placeholder="请输入注册时间条件0否1是" />
        </el-form-item>
        <el-form-item label="注册开始时间范围" prop="memberRegisterStime">
          <el-date-picker clearable size="small"
            v-model="form.memberRegisterStime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择注册开始时间范围">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注册结束时间范围" prop="memberRegisterEtime">
          <el-date-picker clearable size="small"
            v-model="form.memberRegisterEtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择注册结束时间范围">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDiscount, getDiscount, delDiscount, addDiscount, updateDiscount, exportDiscount } from "@/api/system/discount";

export default {
  name: "Discount",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 限时折扣表格数据
      discountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        discountName: null,
        discountTitle: null,
        discountStartDatetime: null,
        discountEndDatetime: null,
        discountCycleType: null,
        cycleStartTime: null,
        cycleEndTime: null,
        cycleMondayUsed: null,
        cycleTuesdayUsed: null,
        cycleWednesdayUsed: null,
        cycleThursdayUsed: null,
        cycleFridayUsed: null,
        cycleSaturdayUsed: null,
        cycleSundayUsed: null,
        cycleDay: null,
        discountQuotaType: null,
        discountQuotaRule: null,
        totalOrders: null,
        totalAmount: null,
        selectedWipeZero: null,
        selectedWipeCents: null,
        isInvalid: null,
        newMember: null,
        memberRegister: null,
        memberRegisterStime: null,
        memberRegisterEtime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询限时折扣列表 */
    getList() {
      this.loading = true;
      listDiscount(this.queryParams).then(response => {
        this.discountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        discountId: null,
        discountName: null,
        discountTitle: null,
        discountStartDatetime: null,
        discountEndDatetime: null,
        discountCycleType: null,
        cycleStartTime: null,
        cycleEndTime: null,
        cycleMondayUsed: null,
        cycleTuesdayUsed: null,
        cycleWednesdayUsed: null,
        cycleThursdayUsed: null,
        cycleFridayUsed: null,
        cycleSaturdayUsed: null,
        cycleSundayUsed: null,
        cycleDay: null,
        discountQuotaType: null,
        discountQuotaRule: null,
        totalOrders: null,
        totalAmount: null,
        selectedWipeZero: null,
        selectedWipeCents: null,
        createTime: null,
        updateTime: null,
        isInvalid: null,
        newMember: null,
        memberRegister: null,
        memberRegisterStime: null,
        memberRegisterEtime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.discountId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加限时折扣";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const discountId = row.discountId || this.ids
      getDiscount(discountId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改限时折扣";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.discountId != null) {
            updateDiscount(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDiscount(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const discountIds = row.discountId || this.ids;
      this.$confirm('是否确认删除限时折扣编号为"' + discountIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDiscount(discountIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有限时折扣数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDiscount(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
